<?php

include_once('online.php');

class public_catalogue extends online
{
	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
		
		// Load the form helper
		$this->load->helper('form');

		// Load the form validation library
		$this->load->library('form_validation');

		// Load the required models
		$this->load->model('volume_model');
		$this->load->model('author_model');
		$this->load->model('author_volume_model');
		$this->load->model('subject_model');
		$this->load->model('subject_volume_model');
		$this->load->model('location_model');
	}
	
	/**
	 * Perform a volume search
	 * @param $table The table to be searched
	 * @param $search_by Name of field to do search on
	 * @param $term Term to search for
	 * @param $offset Offset of results (for pagination)
	 */
	public function searchCatalogue($table='tbl_volume', $search_by='title', $term='ALL', $offset = 0)
	{
		// If the user has done a new search
		if ($this->input->post())
		{
			// Get post results and filter them to rid unacceptable characters
			$table = $this->input->post('table');
			$search_by = $this->input->post('search_by');
			$term = preg_replace('/[^a-zA-Z0-9]/', ' ', $this->input->post('term'));
	
			// If the form has been clicked
			redirect('public/public_catalogue/searchCatalogue/' . $table . '/' . $search_by . '/' . $term );
		}
		
		// Set this page as previous volume search (BUG - searching for a term with > 2 words causes the session data to be lost. I suspect current_url as the culprit)
		$this->session->set_userdata('prev_catalogue_search', preg_replace('[%20]', ' ', current_url()));
		
		// Determine total number of matching results
		if ($table == "tbl_volume")
		{
			$this->data['result_count'] = $this->volume_model->getSearchCount($search_by, $term);
			$this->data['results'] = $this->volume_model->getSearchResults($search_by, $term, $offset, 20);
			$this->data['fields'] = array('access_id'=>'Accession Number', 'call_number'=>'Call Number', 'title'=>'Title', 'catalogue_date'=>'Catalogue Date');
			$this->data['action'] = array('Type'=>'Volume', 'UniqueID'=>'access_id');
		}
		
		else if ($table == "tbl_author")
		{
			$this->data['result_count'] = $this->author_model->getSearchCount($search_by, $term);
			$this->data['results'] = $this->author_model->getSearchResults($search_by, $term, $offset, 20);
			$this->data['fields'] = array('author_id'=>'Author ID', 'author_name'=>'Author Name');
			$this->data['action'] = array('Type'=>'Author', 'UniqueID'=>'author_id');
		}
		
		else if ($table == "tbl_subject")
		{
			$this->data['result_count'] = $this->subject_model->getSearchCount($search_by, $term);
			$this->data['results'] = $this->subject_model->getSearchResults($search_by, $term, $offset, 20);
			$this->data['fields'] = array('subject_id'=>'Subject ID', 'subject_name'=>'Subject Name');
			$this->data['action'] = array('Type'=>'Subject', 'UniqueID'=>'subject_id');
		}
		
		// Setup pagination
		$this->load->library('pagination');
		$config['base_url'] = site_url('public/public_catalogue/searchCatalogue/' . $table . '/' . $search_by . '/' . $term);
		$config['total_rows'] = $this->data['result_count'];
		$config['per_page'] = 20;
		$config['num_links'] = 7;
		$config['uri_segment'] = 7;
		$this->pagination->initialize($config);
		
		// Load the "view_volume" view
		$this->data['main_content'] = $this->load->view('public/public_catalogue/search_catalogue', $this->data, true);
		
		// Load $this->data['main_content'] in the main template
		$this->load->view($this->template, $this->data);
	}
	
	public function viewVolume($access_id)
	{
		// Return the volume that has the specified access_id and then add the volume_table to view
		$this->data['volume'] = $this->volume_model->getVolumeByID( $access_id )->row();
		
		// Return author information for specified volume
		$author_id_array = $this->author_volume_model->getAuthorIDsForVolume( $access_id );
		$this->data['authors'] = $this->author_model->getAuthorsByID( $author_id_array );
		
		// Return subject information for specified volume
		$subject_id_array = $this->subject_volume_model->getSubjectIDsforVolume( $access_id );
		$this->data['subjects'] = $this->subject_model->getSubjectsByID( $subject_id_array );

		// Get list of locations
		$this->data['location'] = $this->location_model->getLocation( $this->data['volume']->location_id )->row();
		
		// Load the "view_volume" view
		$this->data['main_content'] = $this->load->view('public/public_catalogue/view_volume', $this->data, true);
		
		// Load $this->data['main_content'] in the main template
		$this->load->view($this->template, $this->data);
	}
	
	public function viewAuthor($author_id)
	{
		// Set current page as previous URL
		$this->setThisAsPreviousURL( );
	
		// Return author information
		$this->data['author'] = $this->author_model->getAuthorByID($author_id)->row();
	
		// Return volume information for specified author
		$access_id_array = $this->author_volume_model->getAccessIDsForAuthor( $author_id );
		$this->data['volumes'] = $this->volume_model->getVolumesByID( $access_id_array );
		
		// Load the "view_volume" view
		$this->data['main_content'] = $this->load->view('public/public_catalogue/view_author', $this->data, true);
		
		// Load $this->data['main_content'] in the main template
		$this->load->view($this->template, $this->data);
	}
	
	public function viewSubject($subject_id)
	{
		// Return subject information
		$this->data['subject'] = $this->subject_model->getSubjectByID( $subject_id )->row();		
	
		// Return volume information for specified subject
		$access_id_array = $this->subject_volume_model->getAccessIDsForSubject( $subject_id );
		$this->data['volumes'] = $this->volume_model->getVolumesByID( $access_id_array );
		
		// Load the "view_volume" view
		$this->data['main_content'] = $this->load->view('public/public_catalogue/view_subject', $this->data, true);
		
		// Load $this->data['main_content'] in the main template
		$this->load->view($this->template, $this->data);
	}	
}

?>